package com.kafka.kafka.consumer;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import static org.apache.kafka.clients.consumer.ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG;
import static org.apache.kafka.clients.consumer.ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG;

/**
 * Project: tobe-better-coder
 * Package: com.kafka.kafka.consumer
 *
 * @author : Mr.xu
 * @date : 2023/9/12 22:14
 */
public class KafkaConsumerTest {

    public static void main(String[] args) {
        //配置
        Properties properties = new Properties();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"node1:9092");

        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());


//        properties.put(PARTITION_ASSIGNMENT_STRATEGY_CONFIG,)

        //组id
        properties.put(ConsumerConfig.GROUP_ID_CONFIG,"com.test");

        KafkaConsumer<String,String> kafkaConsumer = new KafkaConsumer<>(properties);

        //创建消费者
        List<String> topics = new ArrayList<>();
        topics.add("xuxin");
        kafkaConsumer.subscribe(topics);
        while (true){
            ConsumerRecords<String, String> stringStringConsumerRecord = kafkaConsumer.poll(Duration.ofSeconds(1));
            stringStringConsumerRecord.forEach(item -> {
                System.out.println(item);
            });
        }
    }
}